//
//  RBLedView.m
//  RBDemoProj
//
//  Created by Mac on 2020/11/4.
//  Copyright © 2020 SummerTea. All rights reserved.
//

#import "RBLedView.h"
#import "RBTimer.h"
#import <UIView+YYAdd.h>
#import "RBDefine.h"
#import "RanTool.h"

@interface RBLedView()

@property(nonatomic, strong) UILabel *aL;
@property(nonatomic, strong) UILabel *bL;

@property(nonatomic, strong) RBTimer *timer;

@property(nonatomic, assign) CGFloat labelSpacing;
@property(nonatomic, assign) CGFloat attTitleWidth;

@end

@implementation RBLedView

#pragma mark - Lifecycle

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setup];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self adjust];
}

- (void)adjust
{
    if (self.width <= 0)
    {
        return;
    }
    self.aL.height = self.height;
    self.aL.top = 0;
    self.bL.height = self.height;
    self.bL.top = 0;
    if (self.attTitleWidth > self.width)
    {
        self.aL.width = self.attTitleWidth;
        self.aL.textAlignment = NSTextAlignmentLeft;
        self.aL.left = 0;
        
        self.bL.hidden = NO;
        self.bL.width = self.attTitleWidth;
        self.bL.textAlignment = NSTextAlignmentLeft;
        self.bL.left = self.aL.right + self.labelSpacing;
    }
    else
    {
        self.aL.width = self.width;
        self.aL.textAlignment = self.textAlignment;
        
        self.bL.hidden = YES;
    }
}

#pragma mark - Setup

- (void)setup
{
    self.clipsToBounds = YES;
    self.labelSpacing = 8;
    self.timer = RBTimer.new;
    __weak typeof(self) weak_self = self;
    [self.timer startTimerWithInterval:1.0/60 block:^(float countF, NSInteger countI) {
        if (weak_self.attTitleWidth > weak_self.width && weak_self.width > 0)
        {
            CGFloat move = 1;
            weak_self.aL.left -= move;
            weak_self.bL.left -= move;
            if (weak_self.aL.right <= 0)
            {
                weak_self.aL.left = weak_self.bL.right + weak_self.labelSpacing;
            }
            if (weak_self.bL.right <= 0)
            {
                weak_self.bL.left = weak_self.aL.right + weak_self.labelSpacing;
            }
        }
    }];
}

#pragma mark - Setter

- (void)setAttTitleArray:(NSArray *)attTitleArray
{
    _attTitleArray = attTitleArray;
    NSMutableAttributedString *stringM = NSMutableAttributedString.new;
    for (NSInteger i = 0; i < attTitleArray.count; i++)
    {
        NSAttributedString *attItem = attTitleArray[i];
        [stringM appendAttributedString:attItem];
        if (i != attTitleArray.count - 1)
        {
            [stringM appendAttributedString:[NSAttributedString.alloc initWithString:@" "]];
        }
    }
    self.attTitleWidth = [RanTool sizeWithAttText:stringM].width + 3;
    self.aL.attributedText = stringM;
    self.bL.attributedText = stringM;
    [self adjust];
}

#pragma mark - Getter

- (UILabel *)aL
{
    if (!_aL)
    {
        self.aL = ({
            UILabel *view = UILabel.new;
            [self addSubview:view];
            view;
        });
    }
    return _aL;
}

- (UILabel *)bL
{
    if (!_bL)
    {
        self.bL = ({
            UILabel *view = UILabel.new;
            [self addSubview:view];
            view;
        });
    }
    return _bL;
}

@end
